﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Classes;
using FlightSystemDBLayer;


namespace FlightSystemControlLayer
{
    public class SearcherCtr
    {
        IList<City> listOfCities;
        IList<Flight> listOfFlights;
        ICity dbCity;
        Searcher searcher;
        IFlight dbFlight;

        //Delete if still not used after finishing
        //City cityFrom;
        //City cityTo;
        //int seatsNo;
        //DateTime departureTime;
        //DateTime arrivalTime;
       

        public SearcherCtr()
        {
            dbCity = new DBCity();
            listOfCities = dbCity.GetDBCities();

            dbFlight = new DBFlight(listOfCities); // initiated flight db class with preset list of cities
            

            searcher = new Searcher(); 

        }
        //public City createList()
        //{
        //    City c = new City("Lol", "ddd");
        //    City c2 = new City("to", "ddd");
        //   // c.AddFlight(c2 , 10, 5, new DateTime(2010, 11, 21, 13, 50, 00), new DateTime(2011, 11, 21, 13, 50, 00), 1000, "vulls");

        //    return c;

        //}
        //public ListOfConnections createList()
        //{

        //    int noOfSeatsInOrder = 1;

        //    DateTime departureTime = new DateTime(2010, 11, 21, 13, 50, 00);



        //    SearcherCtr searcherCtr = new SearcherCtr();
        //    BookingCtr booker = new BookingCtr();
        //    City f = searcherCtr.getCityList()[0];
        //    City t = searcherCtr.getCityList()[4];


        //    ListOfConnections l = MakeASearch(f, t, noOfSeatsInOrder, departureTime);

        //    return l;

        //}
        public IList<City> getCityList() { //city list retrieved from database

            return listOfCities;
        }
        

        public ListOfConnections MakeASearch(City cityFrom, City cityTo, int noOfSeats, DateTime dateTime) 
        {
            foreach (City item in listOfCities)
            {
                if (item.Name.Equals(cityFrom.Name))
                {

                    cityFrom = item;
                    //foreach (Flight fl in item.Flights())
                    //{
                    //    //cityFrom.AddFlight(fl.To,fl.NoOfSeats,fl.Id, fl.DepartureTime,fl.ArrivalTime,fl.PriceOfTheOneSeat,fl.Company);
                    //}
                }
                if (item.Name.Equals(cityTo.Name))
                {

                    cityTo = item;
                    //foreach (Flight fl in item.Flights())
                    //{

                    //   // cityFrom.AddFlight(fl.To, fl.NoOfSeats, fl.Id, fl.DepartureTime, fl.ArrivalTime, fl.PriceOfTheOneSeat, fl.Company);
                    //}

                }

            }
            listOfFlights = dbFlight.GetSpecifiedFlights(noOfSeats, dateTime);
            
            searcher.MakeAGraph(listOfCities, listOfFlights);
           

            IGraphImpl list = searcher.ReturnGraph();
            
            
            ListOfConnections conList = list.SearchForConnections(cityFrom, cityTo);

            searcher.fillTheConnections(cityFrom, cityTo, conList);
            
            
            
             Console.ReadLine();
             return conList;
        }
    }
}
